#!/bin/bash

# 更新软件源为清华大学的 Arch Linux 源
echo "更新软件源为清华大学的 Arch Linux 源..."
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak
sed -i '1iServer = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch' /etc/pacman.d/mirrorlist

# 更新系统并安装必要的软件
echo "更新系统并安装必要的软件..."
pacman -Syu --noconfirm
pacman -S --noconfirm openssh xfce4 xfce4-terminal tigervnc dbus-x11 xfe

# 清理缓存
pacman -Sc --noconfirm

# 创建 VNC 目录
mkdir -p ~/.vnc

# 下载必要的脚本文件
echo "下载必要的脚本文件..."
wget https://gitee.com/lic-miao/ubuntu-arm64/raw/master/desktop/xstartup --no-check-certificate -P ~/.vnc/
wget https://gitee.com/lic-miao/ubuntu-arm64/raw/master/desktop/vncserver-start --no-check-certificate -P /usr/local/bin/
wget https://gitee.com/lic-miao/ubuntu-arm64/raw/master/desktop/vncserver-stop --no-check-certificate -P /usr/local/bin/
wget https://gitee.com/lic-miao/ubuntu-arm64/raw/master/desktop/vnc --no-check-certificate -P /usr/local/bin/

# 设置脚本文件的执行权限
chmod +x ~/.vnc/xstartup
chmod +x /usr/local/bin/vncserver-start
chmod +x /usr/local/bin/vncserver-stop
chmod +x /usr/local/bin/vnc

# 输出提示信息
echo " "
echo "如果你想打开 VNC Server，请运行命令 'vnc'。"
echo " "
echo "它将要求您在第一次启动时输入密码。"
echo " "
echo "VNC Server 启动后将运行在端口 127.0.0.1:5901。"
echo " "
echo "您可以使用您喜欢的 VNC 查看器连接到此地址。"
echo " "
echo "连接到端口后将进入 Xfce4 桌面环境。"
echo " "

# 设置 DISPLAY 环境变量
echo "export DISPLAY=:1" >> ~/.bashrc
source ~/.bashrc

# 启动 VNC Server
echo "启动 VNC Server..."
vncserver-start